#!/bin/bash

# Script de instalacao do tabletranscriber
# instala todos os requisitos do projeto, listados no README.
# Supoe-se aqui que o sistema ja tenha uma versao do g++ instalada
# corretamente nos diretorios default para linux

# este arquivo deve ser executado numa pasta com permissoes de root
# no LSD, por causa de problemas de permissoes

# file INSTALL2.sh

# create directory to hold source code
mkdir srcCV_MODULES
chmod 777 srcCV_MODULES

cd srcCV_MODULES

# -------------------------------------------------------------------
# script de instalacao da lib jansson para lidar
# com o json de configuracao do tabletranscriber

wget "http://www.digip.org/jansson/releases/jansson-2.5.tar.gz"
tar -xvzf jansson-2.5.tar.gz
cd jansson-2.5

./configure
make
make check
sudo make install

cd ..

## dependencies of tesseract 3.02 ------------------------------------------
# -------------------------------------------------------------------------

sudo apt-get install autoconf automake libtool
sudo apt-get install libpng12-dev
sudo apt-get install libjpeg62-dev
sudo apt-get install libtiff4-dev
sudo apt-get install zlib1g-dev

# TIFF lib -----------------------------------------------------------------
# ---------------------------------------------------------------------

echo "installing tifflib"

wget http://www.imagemagick.org/download/delegates/tiff-4.0.3.zip
unzip tiff-4.0.3
cd tiff-4.0.3
./configure
make
sudo make install

echo "libtiff installed with success!"

cd ..

## leptonica 1.6.8 -----------------------------------------------------------------------------------------------------------------------------------

wget http://leptonica.googlecode.com/files/leptonica-1.69.tar.gz

echo "installing leptonica-1.69"
tar -xvf leptonica-1.69.tar.gz

cd leptonica-1.69
./configure
sudo make
sudo make install

echo "leptonica installed with success!"

cd ..

## tesseract 3.0.0 -----------------------------------------------------------------------------------------------------------------------------------

# get tesseract
echo "installing tesseract....."

wget http://tesseract-ocr.googlecode.com/files/tesseract-ocr-3.02.02.tar.gz
sudo tar -zxvf tesseract-ocr-3.02.02.tar.gz 
cd tesseract-ocr/
sudo ./configure
sudo make
sudo make install
sudo ldconfig

#mkdir tessdata
sudo mkdir /usr/local/share/tessdata/

# get the english traineddata ----------------------------------------------
sudo wget http://tesseract-ocr.googlecode.com/files/tesseract-ocr-3.02.eng.tar.gz

sudo tar -zxvf tesseract-ocr-3.02.eng.tar.gz 
sudo cp tesseract-ocr/tessdata/eng.* /usr/local/share/tessdata/

echo "eng.traineddata moved to /usr/share/tessdata"

# get the portuguese traineddata ------------------------------------------
sudo wget http://tesseract-ocr.googlecode.com/files/tesseract-ocr-3.02.por.tar.gz

# unpacking por.traineddata
sudo tar -zxvf tesseract-ocr-3.02.por.tar.gz
sudo cp tesseract-ocr/tessdata/por.* /usr/local/share/tessdata/

echo "por.traineddata moved to /usr/share/tessdata"

echo "tesseract installed with success!"

cd ..

## opencv 2.4.1 --------------------------------------------------------------------------------------------------------------------------------------
# based on http://www.ozbotz.org/opencv-installation/

echo "installing opencv 2.4.1..........."

# remove any installed versions of ffmpeg and x264
sudo apt-get remove ffmpeg x264 libx264-dev

# get all dependencies for x264 and ffmpeg
#sudo apt-get update
sudo apt-get install build-essential checkinstall git cmake libfaac-dev libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libtheora-dev libva-dev libvdpau-dev libvorbis-dev libx11-dev libxfixes-dev libxvidcore-dev texi2html yasm zlib1g-dev

# download and install gstreamer
sudo apt-get install libgstreamer0.10-0 libgstreamer0.10-dev gstreamer0.10-tools gstreamer0.10-plugins-base libgstreamer-plugins-base0.10-dev gstreamer0.10-plugins-good gstreamer0.10-plugins-ugly gstreamer0.10-plugins-bad gstreamer0.10-ffmpeg

# download and install gtk
sudo apt-get install libgtk2.0-0 libgtk2.0-dev

# download and install libjpeg
sudo apt-get install libjpeg8 libjpeg8-dev

#download and install x264

wget ftp://ftp.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-20120528-2245-stable.tar.bz2

tar -xvf x264-snapshot-20120528-2245-stable.tar.bz2

cd x264-snapshot-20120528-2245-stable

# configure and build x264 libraries
./configure --enable-shared --enable-pic
make
sudo make install

cd ..

# download and install ffmpeg
wget http://ffmpeg.org/releases/ffmpeg-0.11.1.tar.bz2
tar -xvf ffmpeg-0.11.1.tar.bz2
cd ffmpeg-0.11.1

# configure and build ffmpeg
./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab --enable-shared --enable-pic

make
sudo make install

cd ..

# download and install install a recent version of v4l (video for linux)
wget http://www.linuxtv.org/downloads/v4l-utils/v4l-utils-0.8.8.tar.bz2

tar -xvf v4l-utils-0.8.8.tar.bz2
cd v4l-utils-0.8.8
make
sudo make install

cd ..

# download and install install OpenCV 2.4.2.
wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.2/OpenCV-2.4.2.tar.bz2

tar -xvf OpenCV-2.4.2.tar.bz2

cd OpenCV-2.4.2/
mkdir build
cd build
cmake -D DWITH_JPEG=OFF CMAKE_BUILD_TYPE=RELEASE ..

make
sudo make install

# configure linux: Tell linux where the shared libraries for OpenCV are located by entering the following shell command
export LD_LIBRARY_PATH=/usr/local/lib

# move the file opencv.conf to /etc/ld.so.conf.d/ and make the link
sudo mv opencv.conf /etc/lo.so.conf.d/
sudo ldconfig /etc/ld.so.conf

# add two lines at the end of /etc/bash.bashrc
sudo echo "PKG_CONFIG_PATH=$PKG_CONFIG:/usr/local/lib/pkgconfig" >> /etc/bash.bashrc
sudo echo "export PKG_CONFIG_PATH" >> /etc/bash.bashrc

echo "opencv installed with success!"

cd ../..

## imagemagick 6.8.8 ---------------------------------------------------------------------------------------------------------------------------------

echo "installing imagemagick 6.8.9-3"

# source: http://www.imagemagick.org/Magick++/

wget http://www.imagemagick.org/download/ImageMagick-6.8.9-3.zip

unzip ImageMagick-6.8.9-3.zip

cd ImageMagick-6.8.9-3

./configure
make
sudo make install
sudo ldconfig
make clean

echo "ImageMagick-6 API installed with success!"
